<?php

namespace app\user\controller;

use app\common\model\Domains;
use app\common\model\Quns;
use app\common\model\Shareinfos;
use think\Request;

class Index extends Base
{

    public function index(Request $request)
    {

        if ( $request->isAjax() ){

            $page = input('param.page/d', 1);
            $limit = input('param.limit/d', 20);

            $quns = Quns::where('uid', session('user.id'))->order(['id'=>'desc'])->page($page)->limit($limit)->select();

            if ( count($quns) > 0 ){
                foreach ( $quns as $k=>$v){
                    $v['domain'] = Domains::getOneData(2);
                }
            }

            $data = array(
                'data'      => $quns,
                'count'     => Quns::where('uid', session('user.id'))->count('id'),
                'code'      => 0,
                'msg'       => '',
            );
            return json($data);
        }

        return view();
    }



    // 添加、保存数据
    public function create(Request $request)
    {

        if ( $request->isPost() ){
            $post = trimAll($request->post());
            $post['alias'] = getRandStr(8);
            $post['uid'] = session('user.id');

            // 把换行，替换成||
            $post['qrcode'] = huanhangToStr($post['qrcode']);

            $post['zidingyi'] = [];
            if ( $post['muban'] == 0 ){

                if ( $post['ico'] && $post['type'] && $post['url'] ){
                    foreach ($post['ico'] as $k=>$v){

                        foreach ($post['name'] as $kk=>$vv) {

                            foreach ($post['type'] as $kkk=>$vvv){

                                foreach ($post['url'] as $kkkk=>$vvvv){

                                    if ( $k == $kk && $k == $kkk && $k == $kkkk ){
                                        if ( $v && $vvvv ){
                                            $post['zidingyi'][] = ['ico'=>$v, 'name'=>$vv, 'type'=>$vvv, 'url'=>$vvvv];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }

            $post['zidingyi'] = json_encode($post['zidingyi']);

            unset($post['touxiang']);
            unset($post['file']);
            unset($post['ico']);
            unset($post['name']);
            unset($post['type']);
            unset($post['url']);

            //写入数据库
            if ( Quns::create($post) ){
                createLog('添加微群成功：' . $post['title']);
                success('操作成功');
            }else{
                createLog('添加微群失败：' . $post['title']);
                error('操作失败');
            }

        }

        return view();
    }


    // 编辑、保存数据
    public function edit(Request $request, $id)
    {

        if ( $request->isPost() ){
            $post = trimAll($request->post());
            $post['uid'] = session('user.id');

            // 把换行，替换成||
            $post['qrcode'] = huanhangToStr($post['qrcode']);

            $post['zidingyi'] = [];
            if ( $post['muban'] == 0 ){

                if ( $post['ico'] && $post['type'] && $post['url'] ){
                    foreach ($post['ico'] as $k=>$v){

                        foreach ($post['name'] as $kk=>$vv) {

                            foreach ($post['type'] as $kkk=>$vvv){

                                foreach ($post['url'] as $kkkk=>$vvvv){

                                    if ( $k == $kk && $k == $kkk && $k == $kkkk ){
                                        if ( $v && $vvvv ){
                                            $post['zidingyi'][] = ['ico'=>$v, 'name'=>$vv, 'type'=>$vvv, 'url'=>$vvvv];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }

            $post['zidingyi'] = json_encode($post['zidingyi']);

            unset($post['touxiang']);
            unset($post['file']);
            unset($post['ico']);
            unset($post['name']);
            unset($post['type']);
            unset($post['url']);

            //写入数据库
            if ( Quns::where('id', $id)->update($post) ){
                createLog('编辑微群成功：' . $post['title']);
                success('操作成功');
            }else{
                createLog('编辑微群失败：' . $post['title']);
                error('操作失败');
            }

        }

        //加载数据
        $data = Quns::get($id);

        // 把||，替换成换行
        $data['qrcode'] = str_replace('||', PHP_EOL, $data['qrcode']);

        $data['zidingyi'] = json_decode($data['zidingyi'], true);

        $this->assign('data', $data);
        return view();
    }


	// 分享参数设置
	public function shareinfo(Request $request){
		if ( $request->isPost() ){
			$post = trimAll($request->post());
			$post['uid'] = session('user.id');

			// 获取ID，防止其他用户修改
			$data_u = Shareinfos::where('uid', $post['uid'])->find();

			//写入数据库
			if ( Shareinfos::where('id', $data_u['id'])->update($post) ){
				success('操作成功');
			}else{
				error('操作失败');
			}

		}


		// 如果没有数据，先添加默认数据
		$data = Shareinfos::where('uid', session('user.id'))->find();
		if ( !$data ) {
			$data = [
				'uid'			=> session('user.id'),
				// 聊天间隔时间
				'wechat_time'	=> fission('wechat_time'),
				// 分享背景图（先展示）（推荐群、好友）
				'share_bg'		=> fission('share_bg'),
				// 分享背景图（后更改）（推荐朋友圈）
				'share_bg2'		=> fission('share_bg2'),
				// 分享多少次后更换背景图
				'huan_bg_num'	=> fission('huan_bg_num'),
				// 先分享到群？先圈？
				'share_xian_who'	=> fission('share_xian_who'),
				// 分享到群次数
				'share_qun_num'		=> fission('share_qun_num'),
				// 分享到朋友圈次数
				'share_quan_num'	=> fission('share_quan_num'),

				// 分享触发提示
				'share_alert_0'	=> fission('share_alert_0'),
				// 分享提示1
				'share_alert_1'	=> fission('share_alert_1'),
				// 分享提示2
				'share_alert_2'	=> fission('share_alert_2'),
				// 分享提示3
				'share_alert_3'	=> fission('share_alert_3'),
				// 分享提示4
				'share_alert_4'	=> fission('share_alert_4'),
				// 分享提示5
				'share_alert_5'	=> fission('share_alert_5'),
				// 分享提示6
				'share_alert_6'	=> fission('share_alert_6'),
				//分享完成二维码提示
				'share_end_msg'	=> fission('share_end_msg'),
			];
			Shareinfos::create($data);
		}

		$this->assign('data', $data);
		return view();

	}



}
